﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Simple.Common.Core.Hosting
{
    /// <summary>
    /// IServiceProvider是.NET Core中的服务容器，他能够访问到程序注入的环境变量
    /// </summary>
    public class ServiceProviderHelper
    {
        /// <summary>
        /// ServiceProvider
        /// </summary>
        public static IServiceProvider ServiceProvider { get; set; }
        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>()
        {
            try
            {
                return (T)ServiceProvider?.GetService(typeof(T));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
